package com.gdip.service.impl;


import com.gdip.common.dtos.ResponseResult;
import com.gdip.common.enums.AppHttpCodeEnum;
import com.gdip.dto.ProblemDTO;
import com.gdip.dto.ReplyDTO;
import com.gdip.mapper.*;
import com.gdip.po.*;
import com.gdip.service.ProblemService;
import com.gdip.vo.ProblemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemServiceImpl implements ProblemService {

    @Autowired
    ProblemTypesMapper problemTypesMapper;
    @Autowired
    ProblemMapper problemMapper;
    @Autowired
    ReplyMapper replyMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SchoolClassMapper schoolClassMapper;
    @Autowired
    CommentMapper commentMapper;

    @Override
    public ResponseResult addProblem(ProblemDTO problemDTO, Integer userId) {

        ProblemPo problemPo = new ProblemPo();

        BeanUtils.copyProperties(problemDTO,problemPo);

//        Integer typeIdByName = problemTypesMapper.findTypeIdByName(problemDTO.getType());

        problemPo.setTypeId(problemDTO.getTypeId());
        problemPo.setClassId(problemDTO.getTypeId());
        problemPo.setStatus("未解决");
        problemPo.setUserId(userId);

        int insertedRows = problemMapper.insertProblem(problemPo);

        if (insertedRows > 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public ResponseResult listProblem(Integer userId) {

        List<ProblemPo> problemPos = problemMapper.selectProblemsByUserIdAndStatus(userId,"已完成");
        List<ProblemVO> problemVOS = new ArrayList<>();

        for (ProblemPo problemPo : problemPos) {
            ProblemVO problemVO = new ProblemVO();

            problemVO.setCreateTime(problemPo.getCreateTime());
            problemVO.setTitle(problemPo.getTitle());
            problemVO.setDetail(problemPo.getDetail());

            CommentPo commentByProblemId = commentMapper.getCommentByProblemId(problemPo.getId());

            problemVO.setScore(commentByProblemId.getScore());

            problemVOS.add(problemVO);
        }

        if (problemPos != null){
            return ResponseResult.okResult(problemVOS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult replyProblem(ReplyDTO replyDTO, Integer userId) {

        ReplyPo replyPo = new ReplyPo();
        replyPo.setUserId(userId);
        replyPo.setProblemId(Integer.valueOf(replyDTO.getProblemId()));
        replyPo.setContent(replyDTO.getContent());

        replyPo.setCreateTime(LocalDateTime.now());
        replyPo.setUpdateTime(LocalDateTime.now());

//        // 判断是否二级评论
//        if (replyDTO.getParentId()!=null){
//            replyPo.setParentId(Integer.valueOf(replyDTO.getParentId()));
//        }

        int insertedRows = replyMapper.replyProblem(replyPo);


        ProblemPo byId = problemMapper.findById(replyPo.getProblemId());
        byId.setStatus("已回复");
        problemMapper.updateProblem(byId);


        if (insertedRows > 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult listTeacherProblems(Integer userId) {
        UserPo userById = userMapper.findById(userId);
        Integer uClass = userById.getUClass();

        List<ProblemPo> problemIdByClassId = problemMapper.getProblemIdByClassId(uClass);

        if (problemIdByClassId.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }else {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }

    @Override
    public ResponseResult getClasses() {

        List<SchoolClass> classes = schoolClassMapper.findAllSchoolClasses();

        if (classes.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }else {
            return ResponseResult.okResult(classes);
        }
    }

    @Override
    public ResponseResult getTypes() {
        List<ProblemTypesPo> types = problemTypesMapper.findAllTypes();

        if (types.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }else {
            return ResponseResult.okResult(types);
        }
    }

    @Override
    public ResponseResult getHistory(Integer userId) {

        return null;
    }

    @Override
    public ResponseResult getProblemReply(Integer userId) {

        List<ProblemPo> problemPos = problemMapper.selectProblemsByUserIdAndStatus(userId, "待回复");

        List<ProblemPo> problemPos1 = problemMapper.selectProblemsByUserIdAndStatus(userId, "已回复");

        for (ProblemPo problemPo : problemPos1) {
            problemPos.add(problemPo);
        }

        List<ProblemVO> problemVOS = new ArrayList<>();

        for (ProblemPo problemPo : problemPos) {
            ProblemVO problemVO = new ProblemVO();

            problemVO.setId(problemPo.getId());
            problemVO.setTitle(problemPo.getTitle());
            problemVO.setDetail(problemPo.getDetail());
            problemVO.setStatus(problemPo.getStatus());

            UserPo byId = userMapper.findById(userId);
            String schoolClassById = schoolClassMapper.getSchoolClassById(byId.getUClass());
            problemVO.setUsername(byId.getUsername());
            problemVO.setUClass(schoolClassById);

            ReplyPo replyPo = replyMapper.findRepliesByProblemId(problemPo.getId()).get(0);


            if (replyPo != null){
                problemVO.setCreateTime(replyPo.getCreateTime());
                problemVO.setReply(replyPo.getContent());
            }

            problemVOS.add(problemVO);
        }


        return ResponseResult.okResult(problemVOS);
    }

    @Override
    public ResponseResult listReply(Integer userId) {

        UserPo byId = userMapper.findById(userId);
        List<ProblemPo> problemPos = problemMapper.selectProblemsByClassIdAndStatus(byId.getUClass(), "待回复");

        List<ProblemVO> problemVOS = new ArrayList<>();
        for (ProblemPo problemPo : problemPos) {
            ProblemVO problemVO = new ProblemVO();

            problemVO.setTitle(problemPo.getTitle());
            problemVO.setDetail(problemPo.getDetail());
            problemVO.setStatus(problemPo.getStatus());
            problemVO.setId(problemPo.getId());
            problemVO.setCreateTime(problemPo.getCreateTime());

            UserPo problemUser = userMapper.findById(problemPo.getUserId());
            String schoolClassById = schoolClassMapper.getSchoolClassById(problemUser.getUClass());
            problemVO.setUsername(problemUser.getUsername());
            problemVO.setUClass(schoolClassById);


            problemVOS.add(problemVO);
        }

        return ResponseResult.okResult(problemVOS);
    }

    @Override
    public ResponseResult rollback(Integer id) {
        ProblemPo byId = problemMapper.findById(id);

        byId.setStatus("回退");

        int i = problemMapper.updateProblem(byId);
        if (i>0){
            return ResponseResult.okResult(null);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public ResponseResult findProblemsByTitleAndCreateTime(String title) {
        List<ProblemPo> problemPos = problemMapper.selectProblemsByTitleAndCreateTime(title);

        return ResponseResult.okResult(problemPos);
    }

    @Override
    public ResponseResult getBackProblems() {
        List<ProblemPo> problemAllReturnStatus = problemMapper.getProblemAll_returnStatus("回退");


        List<ProblemVO> problemVOS = new ArrayList<>();
        for (ProblemPo problemPo : problemAllReturnStatus) {
            ProblemVO problemVO = new ProblemVO();

            problemVO.setTitle(problemPo.getTitle());
            problemVO.setDetail(problemPo.getDetail());
            problemVO.setStatus(problemPo.getStatus());
            problemVO.setId(problemPo.getId());
            problemVO.setCreateTime(problemPo.getCreateTime());

            String typeNameById = problemTypesMapper.findTypeNameById(problemPo.getTypeId());
            problemVO.setType(typeNameById);

            String schoolClassById = schoolClassMapper.getSchoolClassById(problemPo.getClassId());
            problemVO.setUClass(schoolClassById);

            UserPo byId = userMapper.findById(problemPo.getUserId());
            problemVO.setUsername(byId.getUsername());


            problemVOS.add(problemVO);
        }


        return ResponseResult.okResult(problemVOS);
    }

    @Override
    public ResponseResult updateClass(ProblemDTO problemDTO) {

        ProblemPo byId = problemMapper.findById(problemDTO.getId());

        Integer idByClassName = schoolClassMapper.findIdByClassName(problemDTO.getUclass());

        byId.setClassId(idByClassName);
        byId.setStatus("待回复");
        int i = problemMapper.updateProblem(byId);

        if (i>0){
            return ResponseResult.okResult(null);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }


}
